﻿using Microsoft.Xaml.Behaviors;
using System.Windows;
using System.Windows.Controls;

namespace MeterReader.Behaviors
{
    public class MaxMinBehavior : Behavior<Button>
    {
        public Window TartgetWindow
        {
            get { return (Window)GetValue(TartgetWindowProperty); }
            set { SetValue(TartgetWindowProperty, value); }
        }

        public static readonly DependencyProperty TartgetWindowProperty = DependencyProperty.Register(
            "TartgetWindow",
            typeof(Window),
            typeof(MaxMinBehavior),
            new PropertyMetadata()
        );

        protected override void OnAttached()
        {
            AssociatedObject.Click += OnClick;
        }

        protected override void OnDetaching()
        {
            AssociatedObject.Click -= OnClick;
        }

        private void OnClick(object sender, RoutedEventArgs e)
        {
            if (TartgetWindow.WindowState != WindowState.Maximized)
            {
                TartgetWindow.MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight;
                TartgetWindow.WindowState = WindowState.Maximized;
            }
            else
            {
                TartgetWindow.WindowState = WindowState.Normal;
            }
        }
    }
}